package com.notificationdemo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnGeneral;
    private Button btnBigContentView;
    private Button btnClearNotification;
    private Button btnHan;
    private Button btnBigView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btnGeneral = (Button) findViewById(R.id.btnGeneral);

        btnGeneral.setOnClickListener(this);
        btnBigContentView = (Button) findViewById(R.id.btnBigContentView);
        btnBigContentView.setOnClickListener(this);
        btnClearNotification = (Button) findViewById(R.id.btnClearNotification);
        btnClearNotification.setOnClickListener(this);
        btnHan = (Button) findViewById(R.id.btnHan);
        btnHan.setOnClickListener(this);
        btnBigView = (Button) findViewById(R.id.btnBigView);
        btnBigView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnGeneral:
                generalNotification();
                break;
            case R.id.btnBigView:
                bigViewNotification();
                break;
            case R.id.btnBigContentView:
                remoteViewsNotification();
                break;
            case R.id.btnClearNotification:
                //根据清除通知
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                manager.cancel(13);
                break;
            case R.id.btnHan:
                hangNotification();
                break;

        }
    }

    //发送普通通知
    private void generalNotification() {
        //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        Intent intent = new Intent();
        //设置通知栏点击后启动IPCServiceActivity
        intent.setComponent(new ComponentName("com.bindservice", "com.bindservice.IPCServiceActivity"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("普通的通知")
                .setContentText("......普通通知内容......")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.hert))
                .setAutoCancel(true)
                .setOngoing(false)
                .setNumber(1)
                .setTicker("通知来了......")
                .build();
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(12, notification);
    }

    //自定义Notification
    private void remoteViewsNotification() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, 0);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentText("自定义布局消息")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.hert))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .build();
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.rv);
        //notification.bitentView = remoteViews;//折叠效果显示
        notification.contentView=remoteViews;
        manager.notify(10, notification);
    }

    //浮动Notification
    private void hangNotification() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.rv);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("悬挂式通知")
                .setContentText("...悬停消息...")
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.hert))
                .setContentIntent(pendingIntent)
                .setFullScreenIntent(pendingIntent, true)//悬停通知属性设置
                .build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(9, notification);
    }

    //大视图通知
    private void bigViewNotification() {
        //构建Action Button的PendingIntent
        Intent intentA = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        PendingIntent piA = PendingIntent.getActivity(this, 0, intentA, 0);

        Intent intentB=new Intent();
        intentB.setComponent(new ComponentName("com.bindservice", "com.bindservice.IPCServiceActivity"));
        PendingIntent piB = PendingIntent.getActivity(this, 0, intentB, 0);

        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("大视图通知标题")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("大视图内容部分"))
                .addAction(android.R.drawable.alert_light_frame,"baidu",piA)
                .addAction(android.R.drawable.sym_def_app_icon,"service",piB)
                .build();
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(5,notification);
    }
}
